﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Quark.Profiler.Framework;

namespace Quark.Profiler.Framework.Test
{
    /// <summary>
    /// ProfilingTestProject功能性单元测试
    /// </summary>
    [TestClass]
    public class ProfilingTestProjectUnitTest
    {
        public ProfilingTestProjectUnitTest()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        //
        // 编写测试时，还可使用以下附加属性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestCaseManagementTest()
        {
            IProfilingContext context = null;
            ProfilingTestProject project = new ProfilingTestProject(context);

            //添加用例测试
            ProfilingTestCase testCase1 = project.AppendNewTestCase("用例1", "用例1描述");
            Assert.IsTrue((project.Cases.Length == 1) && (project.Cases[0] == testCase1), "AppendNewTestCase(string, string)方法空集合添加用例测试失败");
            ProfilingTestCase testCase2 = project.AppendNewTestCase("用例2", "用例2描述");
            Assert.IsTrue((project.Cases.Length == 2) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                , "AppendNewTestCase(string, string)方法非空集合添加用例测试失败");

            //集合元素移动测试
            project.MoveTestCaseUp(testCase1);
            Assert.IsTrue((project.Cases[0] == testCase1) && (project.Cases[1] == testCase2), "对集合第一个元素执行MoveTestCaseUp(...)方法测试失败");
            project.MoveTestCaseUp(testCase2);
            Assert.IsTrue((project.Cases[0] == testCase2) && (project.Cases[1] == testCase1), "对集合非第一个元素执行MoveTestCaseUp(...)方法测试失败");
            project.MoveTestCaseDown(testCase2);
            Assert.IsTrue((project.Cases[0] == testCase1) && (project.Cases[1] == testCase2), "对集合第一个元素执行MoveTestCaseDown(...)方法测试失败");
            project.MoveTestCaseDown(testCase2);
            Assert.IsTrue((project.Cases[0] == testCase1) && (project.Cases[1] == testCase2), "对集合最后一个元素执行MoveTestCaseDown(...)方法测试失败");

            //进行更多元素的测试
            ProfilingTestCase testCase3 = project.AppendNewTestCase("用例3", "用例3描述");
            Assert.IsTrue((project.Cases.Length == 3) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2) 
                && (project.Cases[2] == testCase3), 
                "AppendNewTestCase(string, string)方法非空集合添加用例测试失败");
            ProfilingTestCase testCase4 = project.AppendNewTestCase("用例4", "用例4描述");
            Assert.IsTrue((project.Cases.Length == 4) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4),
                "AppendNewTestCase(string, string)方法非空集合添加用例测试失败");
            ProfilingTestCase testCase5 = project.AppendNewTestCase("用例5", "用例5描述");
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "AppendNewTestCase(string, string)方法非空集合添加用例测试失败");

            Assert.IsTrue((testCase1 != testCase2) && (testCase1 != testCase3) && (testCase1 != testCase4) && (testCase1 != testCase5));

            //集合元素移动测试
            project.MoveTestCaseUp(testCase1);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "对集合第一个元素执行MoveTestCaseUp(...)方法测试失败");
            project.MoveTestCaseDown(testCase1);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase2) && (project.Cases[1] == testCase1)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "对集合第一个元素执行MoveTestCaseDown(...)方法测试失败");
            project.MoveTestCaseUp(testCase1);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "对集合中间的元素执行MoveTestCaseUp(...)方法测试失败");
            project.MoveTestCaseDown(testCase5);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "对集合最后一个元素执行MoveTestCaseDown(...)方法测试失败");
            project.MoveTestCaseUp(testCase5);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase5) && (project.Cases[4] == testCase4),
                "对集合最后一个元素执行MoveTestCaseUp(...)方法测试失败");
            project.MoveTestCaseDown(testCase5);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "对集合中间的元素执行MoveTestCaseDown(...)方法测试失败");
            project.MoveTestCaseDown(testCase2);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase3)
                && (project.Cases[2] == testCase2) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "对集合中间的元素执行MoveTestCaseDown(...)方法测试失败");
            project.MoveTestCaseUp(testCase2);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "对集合中间的元素执行MoveTestCaseUp(...)方法测试失败");
            project.MoveTestCaseUp(testCase4);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase4) && (project.Cases[3] == testCase3) && (project.Cases[4] == testCase5),
                "对集合中间的元素执行MoveTestCaseUp(...)方法测试失败");
            project.MoveTestCaseDown(testCase4);
            Assert.IsTrue((project.Cases.Length == 5) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4) && (project.Cases[4] == testCase5),
                "对集合中间的元素执行MoveTestCaseDown(...)方法测试失败");

            //删除集合测试
            project.RemoveTestCase(testCase5);
            Assert.IsTrue((project.Cases.Length == 4) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase2)
                && (project.Cases[2] == testCase3) && (project.Cases[3] == testCase4),
                "删除集合元素RemoveTestCase(...)方法测试失败");
            project.RemoveTestCase(testCase2);
            Assert.IsTrue((project.Cases.Length == 3) && (project.Cases[0] == testCase1) && (project.Cases[1] == testCase3)
                && (project.Cases[2] == testCase4),
                "删除集合元素RemoveTestCase(...)方法测试失败");
            project.RemoveTestCase(testCase1);
            Assert.IsTrue((project.Cases.Length == 2) && (project.Cases[0] == testCase3) && (project.Cases[1] == testCase4),
                "删除集合元素RemoveTestCase(...)方法测试失败");

            //清空集合测试
            project.ClearTestCases();
            Assert.IsTrue((project.Cases.Length == 0), "清空集合ClearTestCases()方法测试失败");
        }
    }
}
